﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContracts;
using System.ServiceModel;

namespace Presention
{
    public partial class UseList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }


        private void Bind()
        {
            using (ServiceAgent UserServiceAgent = new ServiceAgent())
            {
                List<DataContracts.UserDTO> userList = UserServiceAgent.ServiceClient.GetAllUser();
                UserListGridView.DataSource = userList;
                UserListGridView.DataBind();
                DepartmentDropDownList.DataSource = UserServiceAgent.ServiceClient.GetAllDepartment();
                DepartmentDropDownList.DataTextField = "DepartmentName";
                DepartmentDropDownList.DataValueField = "DepartmentId";
                DepartmentDropDownList.DataBind();
                DepartmentDropDownList.Items.Insert(0, new ListItem("Please Select", "0"));
            }
        }

        protected void CreateNewUserButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateNewUser.aspx");
        }

        /// <summary>
        /// Implement Deleting User
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UserListGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int UserId = Convert.ToInt32((UserListGridView.Rows[e.RowIndex].FindControl("UserIdlabel") as Label).Text);
            using (ServiceAgent UserServiceAgent = new ServiceAgent())
            {
                UserServiceAgent.ServiceClient.DeleteUser(UserId);
                Response.Write("<script>alert('Delete successfully');window.location.href='UserList.aspx'</script>");
                Bind();
            }
        }

        protected void UserListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton delete = e.Row.FindControl("DeleteLinkButton") as LinkButton;
                if (delete.Text == "Delete")
                    delete.Attributes.Add("onclick", "return confirm('Do you want to delete it?')");
            }
        }


        protected void UserListGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int UserId = Convert.ToInt32((UserListGridView.Rows[e.NewEditIndex].FindControl("UserIdlabel") as Label).Text);
            Response.Redirect("EditUser.aspx?UserId=" + UserId);
        }

        /// <summary>
        /// Search User by UserName and Department
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SearchButton_Click(object sender, EventArgs e)
        {
            if (FirstNameTextBox.Text == "" && LastNameTextBox.Text == "" && DepartmentDropDownList.SelectedValue == "0")
            {
                using (ServiceAgent UserServiceAgent = new ServiceAgent())
                {
                    UserListGridView.DataSource = UserServiceAgent.ServiceClient.GetAllUser();
                    UserListGridView.DataBind();
                    DepartmentDropDownList.DataSource = UserServiceAgent.ServiceClient.GetAllDepartment();
                    DepartmentDropDownList.DataTextField = "DepartmentName";
                    DepartmentDropDownList.DataValueField = "DepartmentId";
                    DepartmentDropDownList.DataBind();
                    DepartmentDropDownList.Items.Insert(0, new ListItem("Please Select", "0"));
                }
            }

            else
            {
                string firstname = FirstNameTextBox.Text;
                string lastname = LastNameTextBox.Text;
                int departmentid = Convert.ToInt32(DepartmentDropDownList.Text);
                using (ServiceAgent UserServiceAgent = new ServiceAgent())
                {

                    List<UserDTO> list = UserServiceAgent.ServiceClient.SearchUser(departmentid, firstname, lastname);
                    if (list.Count == 0)
                    {
                        UserListGridView.DataSource = null;
                        UserListGridView.DataBind();
                        Response.Write("<script>alert('Sorry!Do not have matching User.')</script>");

                    }
                    else
                    {
                        UserListGridView.DataSource = list;

                        UserListGridView.DataBind();
                    }

                }
            }
        }
    }
}









